package com.hbzhit.common.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hbzhit.common.enums.NmNoticeCategoryEnum;
import com.hbzhit.modules.nm.entity.NoticeEntity;
import com.hbzhit.modules.nm.service.NoticeService;
import com.hbzhit.modules.sys.service.SysRoleMenuService;
import com.hbzhit.modules.sys.service.SysUserService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class NoticeUtils {

    private static final NoticeService noticeService;
    private static final SysRoleMenuService roleMenuService;
    private static final SysUserService sysUserService;
    static {
        noticeService = (NoticeService) SpringContextUtils.getBean("noticeService");
        roleMenuService = (SysRoleMenuService) SpringContextUtils.getBean("sysRoleMenuService");
        sysUserService = (SysUserService) SpringContextUtils.getBean("sysUserService");
    }

    /**
     * 发送通知（单人）
     * @param content   通知内容
     * @param userId    目标用户Id
     */
    public static void sendNoticeToUser(String content, Long userId) {
        Collection<Long> idList = new ArrayList<Long>(){{ add(userId); }};
        sendNoticeToUsers(content, idList);
    }

    /**
     * 发送通知（多人）
     * @param content   通知内容
     * @param userIds   目标用户Id集合
     */
    public static void sendNoticeToUsers(String content, Collection<Long> userIds) {
        noticeService.saveNotice(NmNoticeCategoryEnum.NOTICE.getValue(), null, content, null, userIds, null, null);
    }

    /**
     * 发送代办（多人）
     * @param content   通知内容
     * @param source    来源
     * @param perms     权限标识
     * @param userIds   目标用户Id集合
     */
    public static void sendTodoToUsers(String content, String source, String perms, Collection<Long> userIds) {
        noticeService.saveNotice(NmNoticeCategoryEnum.TODO.getValue(), null, content, null, userIds, null, StrUtil.format("{}_{}", perms, source));
    }

    /**
     * 发送代办（单人）
     * @param content   通知内容
     * @param source    来源
     * @param perms     权限标识
     * @param userId    目标用户Id
     */
    public static void sendTodoToUser(String content, String source, String perms, Long userId) {
        Collection<Long> idList = new ArrayList<Long>(){{ add(userId); }};
        sendTodoToUsers(content, source, perms, idList);
    }

    /**
     * 发送代办（权限标识）
     * @param content   通知内容
     * @param source    来源
     * @param perms     权限标识
     */
    public static void sendTodoToPerms(String content, String source, String perms) {
        List<Long> userIds = roleMenuService.queryPermUserIdList(new String[]{perms});
        sendTodoToUsers(content, source, perms, userIds);
    }

    public static void readNotice(String source, String perms) {
        List<NoticeEntity> noticeList = noticeService.list(new LambdaQueryWrapper<NoticeEntity>()
                .eq(NoticeEntity::getSource, StrUtil.format("{}_{}", perms, source))
        );
        noticeList.forEach(notice -> {
            List<Long> noticeIds = new ArrayList<>();
            noticeIds.add(notice.getId());
            noticeService.readNotice(noticeIds, notice.getTargetUserId());
        });
    }
}
